package homegroup.inc.dao;

import homegroup.inc.dto.Book;

import javax.ejb.Stateless;
import javax.persistence.*;
import java.util.List;

@Stateless
public class BookDAO extends BaseDAO {
    private Book book = null;

    public void create(Book aBook) {
        entityManager.persist(aBook);
    }

    public Book findById(int key) {
        book = entityManager.find(Book.class, key);
        return book;
    }

    public List<Book> findByISBN(String isbn) {
        String q = "select b from Book b where b.ISBN like '%" + isbn + "%'";
        Query query = entityManager.createQuery(q);
        List<Book> books = query.getResultList();
        return books;
    }

    public void update(Book aBook) {
        entityManager.merge(aBook);
    }

    public void delete(Book aBook) {
        Book  book = entityManager.merge(aBook);
    }
}
